# TF expression ROC analysis
# 2024-11-20

library(pROC)
library(tidyverse)
library(readxl)

prefilename <- "D:/Work/TF/Ph1/" # for working computer
# prefilename <- "." # for testing

data_file <- "/TF_Analysis.xlsx"


tumor2en <- function(tumor_cn) {
  # Translate the Chinese tumor into English
  # Args:
  #   tumor_cn: a list of tumor of Chinese
  # Returns:
  #   a list of tumor of English
  
  # Create a mapping dictionary
  tumor_map <- c(
    "宫颈癌" = "CC",
    "胰腺癌" = "PDAC",
    "尿路上皮癌" = "UC",
    "卵巢癌" = "OC",
    "鼻咽癌" = "NPC",
    "头颈鳞癌" = "HNSCC",
    "输卵管癌" = "OC", # 输卵管癌 视为 OC
    "前列腺癌" = "Prostate"
  )
  
  # Use str_replace_all() function to replace all Chinese tumors with English
  tumor_en <- str_replace_all(tumor_cn, tumor_map)
  
  return(tumor_en)
}


data_file <- paste0(prefilename, data_file)

patients <- read_xlsx(data_file,
                      sheet = "TF_expression",
                      range = cell_cols("A:G")
)

glimpse(patients)  

pts <- patients |> 
  na.omit() |> 
  filter(BOR != "NE") |> 
  mutate(Tumor = tumor2en(Tumor),
         Response = if_else(BOR %in% c("CR", "cr", "PR", "pr"), 1, 0),
         Stable = if_else(BOR %in% c("CR", "PR", "SD"), 1, 0))

pdac <- pts |> 
  filter(Tumor == "PDAC")

oc <- pts |> 
  filter(Tumor == "OC")


cc <- pts |> 
  filter(Tumor == "CC")

tf_roc <- roc(Stable ~ TF, data = pdac)
auc(tf_roc)
plot(tf_roc,
     print.auc = TRUE,
     print.thres = "best")
